package cn.dakaqi.volunteer.tag;

import cn.dakaqi.entities.Platform;
import cn.dakaqi.utils.Identities;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;

/**
 * Created by chunyang on 2016/5/29.
 */
@Service(value = "platformService")
@Transactional
public class PlatformServiceImpl implements PlatformService
{
    @Autowired
    PlatformDao platformDao;

    @Override
    public void save(Platform platform) throws ServiceRuntimeException
    {
        platform.setCode(Identities.randomBase62(10).toUpperCase());
        this.platformDao.save(platform);
    }

    @Override
    public void update(Platform platform) throws ServiceRuntimeException
    {
        this.platformDao.save(platform);
    }

    @Override
    public void updateStatus(Long id, int status) throws ServiceRuntimeException
    {
        Platform platform = findOne(id);
        if(null != platform)
        {
            platform.setStatus(status);
            update(platform);
        }
    }

    @Override
    public void updateStatus(String code, int status) throws ServiceRuntimeException
    {
        Platform platform = findByCode(code);
        if(null != platform)
        {
            platform.setStatus(status);
            update(platform);
        }
    }

    @Override
    public Page<Platform> findAll(int pageNumber,int status) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 20, sort);

        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, status));
        Specification<Platform> spec = DynamicSpecifications.bySearchFilter(filters.values(), Platform.class);
        return this.platformDao.findAll(spec, pageRequest);
    }

    @Override
    public Platform findOne(Long id) throws ServiceRuntimeException
    {
        return this.platformDao.findOne(id);
    }

    @Override
    public Platform findByCode(String code) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("code", new SearchFilter("code", SearchFilter.Operator.EQ, code));
        Specification<Platform> spec = DynamicSpecifications.bySearchFilter(filters.values(), Platform.class);
        List<Platform> list = this.platformDao.findAll(spec);
        if(null != list && list.size()>0)
            return list.get(0);

        return null;
    }
}
